package cn.tedu.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Base64;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.tedu.service.UserService;
import cn.tedu.service.impl.UserServiceImpl;
import cn.tedu.util.CommonValue;

public class UserLoginServlet extends HttpServlet {
	
	public UserLoginServlet() {
		System.out.println("UserLoginServlet-->constructor()");
	}	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("UserLoginServlet-->doGet()");
		boolean f=false;
		/*
		InputStream is=request.getInputStream();
		String content="";
		DataInputStream dt=new DataInputStream(is);
		byte[] buffer=new byte[1024];
		int len=-1;
		while((len=dt.read(buffer))!=-1) {
		    //把网络输入流中的所有的数据转换成字符串,字符串的编码为英文编码ISO-8859-1
			content+=new String(buffer,0,len,"ISO-8859-1");
		}
		dt.close();
		System.out.println("content-->"+content);
		*/
		//1.获取数据
		String uname=request.getParameter("userName");
		//uname=new String(uname.getBytes("ISO-8859-1"),"UTF-8");
		String upwd=request.getParameter("userPassword");
		System.out.println("userName="+uname+"    userPassword="+upwd);
		
		//获取用户输入的验证码
		String validateCode=request.getParameter("validateCode");
		
		//获取记住用户名
		String rememberName=request.getParameter("remembername");
		//获取10天自动登录
		String autologin=request.getParameter("autologin");
		
		/*由同学 自行 输出查看结果
		System.out.println(request.getRemoteAddr());
		System.out.println(request.getCharacterEncoding());
		System.out.println(request.getContentLength());
		System.out.println(request.getContentType());
		System.out.println(request.getContextPath());
		System.out.println(request.getLocalAddr());
		System.out.println(request.getLocalName());
		System.out.println(request.getLocalPort());
		System.out.println(request.getMethod());
		System.out.println(request.getPathInfo());
		System.out.println(request.getProtocol());
		System.out.println(request.getQueryString());
		System.out.println(request.getRequestURI());
		System.out.println(request.getHttpServletMapping().getPattern());
		System.out.println(request.getInputStream());
		*/
		//2.调用登录业务逻辑
		//获取session对象
		HttpSession session=request.getSession();
		//System.out.println(session);
		//System.out.println(session.getAttribute("code"));
		String code=session.getAttribute("code").toString();
		
		//从请求对象中获取,客户端发送给服务端的所有的cookie
		Cookie[] cookies=request.getCookies();
		if(cookies!=null) {
			for(Cookie c: cookies) {
				if("autologin".equals(c.getName())) {
					f=true;//说明请求中带有autologin这个cookie
				}
			}
		}
		
		if(f || code.equalsIgnoreCase(validateCode)) {
			//说明验证码录入正确,可以做正常的登录业务了
			UserService userService=new UserServiceImpl();
			boolean flag=userService.login(uname, upwd);
			//3.根据业务的结果做跳转工作
			if(flag) {				
				session.setAttribute("user_name", uname);
				
				//处理记住用户名,打上对勾,则需要创建cookie并响应给客户端存储cookie的数据
				if("true".equals(rememberName)) {
					//创建cookie对象
					Cookie cookie=new Cookie("remname",URLEncoder.encode(uname,"UTF-8"));
					//设置cookie的存储路径
					cookie.setPath(request.getContextPath()+"/");
					//设置cookie的存储时间,秒为单位
					cookie.setMaxAge(60*60*5);//5个小时的秒值
					//把cookie对象存储给response对象,在响应的时候消息头中会出现set-cookie消息头
					response.addCookie(cookie);
				}
				
				//处理10天自动登录
				if("true".equals(autologin)) {
					//创建cookie对象   cookie对象中存储autologin=张三#123   注意要对密码加密
					Cookie cookie=new Cookie("autologin",URLEncoder.encode(uname,"UTF-8")+"#"+Base64.getEncoder().encodeToString(upwd.getBytes()));
					cookie.setPath(request.getContextPath()+"/");
					cookie.setMaxAge(60*60*24*10);//cookie的存活时间
					response.addCookie(cookie);
				}
				
				//说明登录成功,则跳转到success.jsp
				//response.sendRedirect("success.jsp");
				//说明登录成功,则跳转到查询所有用户的servlet中
				//response.sendRedirect("usershowall.do");
				//说明登录成功,则跳转到分页+模糊的servlet中
				response.sendRedirect("userfindbypage.do");
			}else {
				//说明登录失败,则跳转到login.jsp
				response.sendRedirect("login.jsp");
			}			
		}else {
			//说明验证码录入错误,给登录一个错误提示
			request.setAttribute("messagecode", "验证码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}	
		
		System.out.println("UserLoginServlet.doGet方法执行完毕");
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("UserLoginServlet-->doPost()");
		this.doGet(request, response);
	}

	@Override
	public void destroy() {
		System.out.println("UserLoginServlet-->destroy()");
	}

	@Override
	public void init() throws ServletException {
		System.out.println("UserLoginServlet-->init()");
	}
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("UserLoginServlet-->service()");
		super.service(request, response);
	}
}
